package com.hadron.media.fullsize_image.behaviors 
{
	import temple.debug.IDebuggable;
	import temple.ui.behaviors.AbstractDisplayObjectBehavior;
	import temple.utils.StageProvider;

	import com.greensock.TweenMax;
	import com.greensock.easing.*;

	import flash.display.DisplayObject;
	import flash.events.Event;
	/**
	 * ...
	 * @author Hadron
	 */
	public class FullsizeImageBehavior extends AbstractDisplayObjectBehavior implements IDebuggable
	{
		/*debuggable*/
		private var _debug:Boolean;
		
		public function FullsizeImageBehavior(target:DisplayObject) 
		{
			super(target);
			displayObject.addEventListener(Event.COMPLETE, handleImageLoad);
			displayObject.addEventListener(Event.ADDED_TO_STAGE, handleAdd);
			displayObject.addEventListener(Event.REMOVED_FROM_STAGE, handleRemove);
		}
		
		/* EVENT HANDLERS */
		private function handleStageResize(e:Event):void 
		{
			checkStageDimensions();
		}
		private function handleAdd(e:Event):void 
		{
			//removeEventListener(Event.ADDED_TO_STAGE, handleAdd);
			StageProvider.stage.addEventListener(Event.RESIZE, handleStageResize);
			if (debug) logDebug('added to stage');
		}
		private function handleRemove(e:Event):void 
		{
			//removeEventListener(Event.REMOVED_FROM_STAGE, handleRemove);
			displayObject.stage.removeEventListener(Event.RESIZE, handleStageResize);
		}
		private function handleImageLoad(e:Event):void 
		{
			displayObject.removeEventListener(Event.COMPLETE, handleImageLoad);
			//center object on stage
			displayObject.x = (displayObject.stage.stageWidth - displayObject.width) / 2;
			displayObject.y = (displayObject.stage.stageHeight - displayObject.height) / 2;
			checkStageDimensions();
		}
		private function loop(e:Event):void
		{
			var percX:Number;
			var percY:Number;
			// if any of dimensions does not exceed stage dimensions - block it (center accordingly to stage center)
			var posX:Number = (displayObject.stage.stageWidth - displayObject.width) / 2;
			var posY:Number = (displayObject.stage.stageHeight - displayObject.height) / 2;
			//move target relative to mouseX & mouseY position - within stage bounds
			if (displayObject.width > displayObject.stage.stageWidth)
			{
				percX = Math.round((displayObject.stage.mouseX / displayObject.stage.stageWidth) * 100) / 100;
				posX = -(percX * (displayObject.width - displayObject.stage.stageWidth));
			}
			
			if (displayObject.height > displayObject.stage.stageHeight)
			{
				percY = Math.round((displayObject.stage.mouseY / displayObject.stage.stageHeight) * 100) / 100;
				posY = -(percY * (displayObject.height - displayObject.stage.stageHeight));
			}
			
			TweenMax.to(displayObject, .5, { x:posX, y:posY, ease:Expo.easeOut} );
		}
		
		private function checkStageDimensions():void
		{
			//check image size - if it doesn't fit current stage canvas - add loop to animate
			if (displayObject.height > displayObject.stage.stageHeight || displayObject.width > displayObject.stage.stageWidth)
			{
				displayObject.addEventListener(Event.ENTER_FRAME, loop);
			} else {
				if (displayObject.hasEventListener(Event.ENTER_FRAME)) {
					displayObject.removeEventListener(Event.ENTER_FRAME, loop);
				}
				//position center on stage with a slight delay
				TweenMax.delayedCall(.1, centerContent);
			}
		}
		
		public function centerContent():void
		{
			TweenMax.to(displayObject, .5, { x:(displayObject.stage.stageWidth - displayObject.width) / 2,
											 y:(displayObject.stage.stageHeight - displayObject.height) / 2,
											 ease:Quart.easeOut } );
		}
		/* INTERFACE temple.debug.IDebuggable */		
		public function get debug():Boolean
		{
			return _debug;
		}
		public function set debug(value:Boolean):void
		{
			_debug = value;
		}
	}
}